{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T14:31:41.435277Z",
     "start_time": "2024-05-26T14:31:41.414275Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CTP交易API版本号: v6.7.2_20230913 10:48:10.4926\n",
      "交易前置:tcp://180.168.146.187:10130\n",
      "初始化成功\n",
      "OnFrontConnected\n"
     ]
    }
   ],
   "source": [
    "from openctp_ctp import tdapi\n",
    "\n",
    "from tdspi import CTdSpi, fronts\n",
    "\n",
    "\n",
    "class CTdSpiImpl(CTdSpi):\n",
    "    \"\"\"交易回调实现类\"\"\"\n",
    "\n",
    "    def req_authenticate(self):\n",
    "        print(\"ReqAuthenticate\")\n",
    "\n",
    "        req = tdapi.CThostFtdcReqAuthenticateField()\n",
    "        req.UserID = self._user_id\n",
    "        req.BrokerID = self._broker_id\n",
    "        req.AppID = self._appid\n",
    "        req.AuthCode = self._authcode\n",
    "        ret = self._api.ReqAuthenticate(req, 0)\n",
    "\n",
    "        print(\"\\tRet\", ret)\n",
    "\n",
    "    def OnRspAuthenticate(self,\n",
    "                          pRspAuthenticateField: tdapi.CThostFtdcRspAuthenticateField,\n",
    "                          pRspInfo: tdapi.CThostFtdcRspInfoField,\n",
    "                          nRequestID: int,\n",
    "                          bIsLast: bool\n",
    "                          ):\n",
    "        if pRspInfo is not None and pRspInfo.ErrorID != 0:\n",
    "            print(\"OnRspAuthenticate failed:\", pRspInfo.ErrorID, pRspInfo.ErrorMsg)\n",
    "\n",
    "        print(\"OnRspAuthenticate succeeded.\")\n",
    "\n",
    "\n",
    "spi = CTdSpiImpl(\n",
    "    fronts[\"7x24\"][\"td\"],\n",
    "    '226485',\n",
    "    'sWJedore20@#08)&',\n",
    "    '0000000000000000',\n",
    "    'simnow_client_test',\n",
    "    '9999',\n",
    ")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ed3c737f87faffeb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ReqAuthenticate\n",
      "\tRet 0\n",
      "OnRspAuthenticate succeeded.\n"
     ]
    }
   ],
   "source": [
    "spi.req_authenticate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af796ce1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
